package org.thread.test06.ts.homework.demo01;

/**
 * 男孩线程 - 摘水果
 * @author CSY
 * @date 2025 - 03 - 13
 */
public class BoyThread extends Thread{
    private Basket basket;

    public BoyThread(Basket basket) {
        this.basket = basket;
    }

    public static final String LOCK = "";

    @Override
    public void run() {
        synchronized(LOCK){
            while(true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if(basket.getCount() == basket.getCapacity()){
                    LOCK.notify();
                    System.out.println("篮子满了，进入等待状态");
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }else{
                    basket.setCount(basket.getCount() + 1);
                    System.out.println("小男孩摘了1个苹果");
                }

            }
        }
    }
}
